package com.hos.db.create.ill;

import com.hos.db.entity.ill.IllHistoryEntity;
import com.hos.utile.DLog;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;

import java.util.Date;
import java.util.List;

/**
 * 病史
 * Created by guom on 2018/10/15.
 */
public class IllHistorysTable {
    public static String tableName = "pat_ill_historys";
    public static String tableIllId = "ill_id";//病例id
    public static String tableIllSymptom = "ill_symptom";//症状
    public static String tableIllSymptomMerge = "ill_symptom_merge";//合并症
    public static String tabIllTimeStart = "ill_time_start";//发病开始时间
    public static String tabIllTimeEnd = "ill_time_end";//发病结束时间
    public static String tableIllHistorySmoke = "ill_history_smoke";//个人史
    public static String tableIllHistoryDrink = "ill_history_drink";//个人喝酒史
    public static String tableIllHistoryOperation = "ill_history_operation";//手术史
    public static String tableIllHistoryPart = "ill_history_part";//部位
    public static String tableIllExplain = "ill_explain";//补充说明
    public static String tableCreateTime = "create_time";// 病例创建时间
    //
    public static String careteTable = "create table  "
            + tableName + " ( "
            + tableIllId + "  varchar(200)  , "
            + tableIllSymptom + " varchar(200), "
            + tableIllSymptomMerge + " varchar(200), "
            + tabIllTimeStart + " varchar(200), "
            + tabIllTimeEnd + " varchar(200), "
            + tableIllHistorySmoke + " longText, "
            + tableIllHistoryDrink + " longText, "
            + tableIllHistoryOperation + " longText, "
            + tableIllHistoryPart + " varchar(200), "
            + tableIllExplain + " longText, "
            + tableCreateTime + " datetime "
            + "); ";

    public static boolean onInsert(JdbcTemplate jdbcTemplate, String illId, IllHistoryEntity bean) {
        if (bean == null) {
            bean = new IllHistoryEntity();
        }
        String sql = "INSERT INTO " + IllHistorysTable.tableName
                + "("
                + IllHistorysTable.tableIllId + ","
                + IllHistorysTable.tableIllSymptom + ","
                + IllHistorysTable.tableIllSymptomMerge + ","
                + IllHistorysTable.tabIllTimeStart + ","
                + IllHistorysTable.tabIllTimeEnd + ","
                + IllHistorysTable.tableIllHistorySmoke + ","
                + IllHistorysTable.tableIllHistoryDrink + ","
                + IllHistorysTable.tableIllHistoryOperation + ","
                + IllHistorysTable.tableIllHistoryPart + ","
                + IllHistorysTable.tableIllExplain + ","
                + IllHistorysTable.tableCreateTime
                + ")"
                + " VALUES (?,?,?, ?,?,?, ?,?,?,?,?)";
        Object[] obj = new Object[]{illId, bean.getIllSymptom(), bean.getIllSymptomMerge(),
                bean.getIllTimeStart(), bean.getIllTimeEnd(), bean.getIllHistorySmoke(),
                bean.getIllHistoryDrink(), bean.getIllHistoryOperation(), bean.getIllHistoryPart(),
                bean.getIllExplain(), new Date()};
        jdbcTemplate.update(sql, obj);
        return true;
    }

    //删除
    public static boolean onDlete(JdbcTemplate jdbcTemplate, String illId) {
        String sql = "DELETE  FROM " + IllHistorysTable.tableName
                + " WHERE "
                + IllHistorysTable.tableIllId + " = '" + illId + "'";
        int state = jdbcTemplate.update(sql);
        return state > 0;
    }

    //修改
    public static boolean onUpdate(JdbcTemplate jdbcTemplate, String illId, IllHistoryEntity bean) {
        if (bean == null) {
            return true;
        }
        String update = "";
        String value = bean.getIllSymptom();
        if (value!=null) {
            update += " , " + IllHistorysTable.tableIllSymptom + " = '" + value + "'";
        }
        value = bean.getIllSymptomMerge();
        if (value!=null) {
            update += " , " + IllHistorysTable.tableIllSymptomMerge + " = '" + value + "'";
        }
        value = bean.getIllTimeStart();
        if (value!=null) {
            update += " , " + IllHistorysTable.tabIllTimeStart + " = '" + value + "'";
        }
        value = bean.getIllTimeEnd();
        if (value!=null) {
            update += " , " + IllHistorysTable.tabIllTimeEnd + " = '" + value + "'";
        }
        value = bean.getIllHistorySmoke();
        if (value!=null) {
            update += " , " + IllHistorysTable.tableIllHistorySmoke + " = '" + value + "'";
        }
        value = bean.getIllHistoryDrink();
        if (value!=null) {
            update += " , " + IllHistorysTable.tableIllHistoryDrink + " = '" + value + "'";
        }
        value = bean.getIllHistoryOperation();
        if (value!=null) {
            update += " , " + IllHistorysTable.tableIllHistoryOperation + " = '" + value + "'";
        }
        value = bean.getIllHistoryPart();
        if (value!=null) {
            update += " , " + IllHistorysTable.tableIllHistoryPart + " = '" + value + "'";
        }
        value = bean.getIllExplain();
        if (value!=null) {
            update += " , " + IllHistorysTable.tableIllExplain + " = '" + value + "'";

        }
        if (update.equals("")) {
            return true;
        }
        update = update.replaceFirst(" , ", "");
        String sql = "UPDATE " + IllHistorysTable.tableName
                + " SET "
                + update
                + " WHERE "
                + IllHistorysTable.tableIllId + " = '" + illId + "'";
        int s = jdbcTemplate.update(sql);
        return s > 0;
    }

    public static IllHistoryEntity getResults(JdbcTemplate jdbcTemplate, String illId) {
        String sql = "SELECT * FROM " + IllHistorysTable.tableName
                + " WHERE "
                + IllHistorysTable.tableIllId + " = '" + illId + "'";
        IllHistoryEntity entity = new IllHistoryEntity();
        List<IllHistoryEntity> ills = onQuery(jdbcTemplate, sql);
        if (ills != null && ills.size() > 0) {
            entity = ills.get(0);
        }
        return entity;
    }


    private static List<IllHistoryEntity> onQuery(JdbcTemplate jdbcTemplate, String sql) {
        List<IllHistoryEntity> ills = null;
        try {
            ills = jdbcTemplate.query(sql,
                    ParameterizedBeanPropertyRowMapper.newInstance(IllHistoryEntity.class));
            DLog.e("查询成功：==========");
        } catch (Exception e) {
            DLog.e("查询失败：" + e.getMessage());
        }
        return ills;
    }
}
